import Vue from 'vue';
import VueRouter from 'vue-router';

// import Home from '../views/Home.vue';
const Home = () =>
  import(/* webpackChunkName: "login_home_welcome" */ '../views/Home.vue');
// import Login from '../views/Login.vue';
const Login = () =>
  import(/* webpackChunkName: "login_home_welcome" */ '../views/Login.vue');
// import Welcome from '../views/Welcome.vue';
const Welcome = () =>
  import(/* webpackChunkName: "login_home_welcome" */ '../views/Welcome.vue');

// import Users from '../views/Users.vue';
const Users = () =>
  import(/* webpackChunkName: "user_roles_rights" */ '../views/Users.vue');
// import Roles from '../views/Roles.vue';
const Roles = () =>
  import(/* webpackChunkName: "user_roles_rights" */ '../views/Roles.vue');
// import Rights from '../views/Rights.vue';
const Rights = () =>
  import(/* webpackChunkName: "user_roles_rights" */ '../views/Rights.vue');

// import GoodList from '../views/GoodList.vue';
const GoodList = () =>
  import(/* webpackChunkName: "goodlist_goodadd" */ '../views/GoodList.vue');
// import GoodAdd from '../views/GoodAdd.vue';
const GoodAdd = () =>
  import(/* webpackChunkName: "goodlist_goodadd" */ '../views/GoodAdd.vue');

// import Categories from '../views/Categories.vue';
const Categories = () =>
  import(/* webpackChunkName: "categories_params" */ '../views/Categories.vue');
// import Params from '../views/Params.vue';
const Params = () =>
  import(/* webpackChunkName: "categories_params" */ '../views/Params.vue');

// import Orders from '../views/Orders.vue';
const Orders = () =>
  import(/* webpackChunkName: "orders_reports" */ '../views/Orders.vue');
// import Reports from '../views/Reports.vue';
const Reports = () =>
  import(/* webpackChunkName: "orders_reports" */ '../views/Reports.vue');

// import CeShi from '../views/CeShi.vue';
const CeShi = () =>
  import(/* webpackChunkName: "ceshi" */ '../views/CeShi.vue');

// import NProgress from 'nprogress';
// import 'nprogress/nprogress.css';

// 解决 使用新导航取消了从"/goods/add"到"/goods/user"的导航。
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        name: 'Welcome',
        component: Welcome,
      },
      {
        path: '/users',
        name: 'Users',
        component: Users,
      },
      {
        path: '/roles',
        name: 'Roles',
        component: Roles,
      },
      {
        path: '/rights',
        name: 'Rights',
        component: Rights,
      },
      {
        path: '/goods',
        name: 'GoodList',
        component: GoodList,
      },
      {
        path: '/goods/add',
        name: 'GoodAdd',
        component: GoodAdd,
      },
      {
        path: '/categories',
        name: 'Categories',
        component: Categories,
      },
      {
        path: '/params',
        name: 'Params',
        component: Params,
      },
      {
        path: '/orders',
        name: 'Orders',
        component: Orders,
      },
      {
        path: '/reports',
        name: 'Reports',
        component: Reports,
      },
      {
        path: '/ceshi',
        name: 'CeShi',
        component: CeShi,
      },
    ],
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
  },
];

const router = new VueRouter({
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  // 把跳转路径为/goods开头的 不为/goods/add 的/goods开头截取掉
  if (/^(\/goods)\/+/.test(to.path)) {
    if (to.path === '/goods/add') {
      next();
    } else {
      next(to.path.substring(6));
      // router.push(to.path.substring(6))
    }
  }

  if (to.path === '/login') {
    // NProgress.start();
    next();
  } else {
    const token = localStorage.getItem('token');
    if (token) {
      // NProgress.start();
      next();
    } else {
      // NProgress.start();
      next('/login');
    }
  }
});

// router.afterEach(() => {
//   NProgress.done();
// });
export default router;
